package com.example.wangziqiao.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.wangziqiao.myapplication.adapter.GrouponItemAdapter;
import com.example.wangziqiao.myapplication.model.SearchIn;
import com.example.wangziqiao.myapplication.util.HttpClientUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

public class RecentPromActivity extends AppCompatActivity {

    //查免单团购的URL
    private static final String GROUPON_PROM_INFO_URL ="http://backend.yihaodian.com/groupon/grouponhandler/queryGrouponDiscountData.action?";
    private RecyclerView mRecyclerView;
    private GrouponItemAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recent_prom);
        mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        getPromData();
    }

    //获取数据并加载
    public void getPromData() {
        SearchIn in = new SearchIn();
        in.setStatus(50);
        in.setFreeType(5);
        RequestParams params = buildQueryString(in);
        HttpClientUtils.get(GROUPON_PROM_INFO_URL, params, new JsonHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Toast.makeText(RecentPromActivity.this, "连接超时", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject data) {
                mRecyclerView.setAdapter(mAdapter = new GrouponItemAdapter(data));
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    private RequestParams buildQueryString(SearchIn in) {
        RequestParams params = new RequestParams();
        params.put("mcSiteId",1);
        params.put("rows",50);
        params.put("page",1);
        if(in.getFreeType()!=null){
            params.put("grouponActivityDto.freeType",in.getFreeType());
        }
        if(in.getStatus()!=null){
            params.put("grouponActivityDto.status",in.getStatus());
        }
        return params;
    }
}
